<?php
include_once("Constants.php");
include_once("Entity/survey_result.php");

class survey_result_dao_base {

	function delete_survey_result($id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("DELETE FROM survey_result WHERE survey_result_id = :survey_result_id");
			$stmt->bindParam(':survey_result_id', $id, PDO::PARAM_INT);
			$stmt->execute();
			
			$dbh->commit();
			$dbh = null;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function get_survey_result($id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();
			
			$stmt = $dbh->prepare("SELECT * FROM  survey_result s WHERE s.survey_result_id = :survey_result_id");
			$stmt->bindParam(':survey_result_id', $id, PDO::PARAM_INT);
			$stmt->execute();
			
			$s = null;
			if($row = $stmt->fetch())
			{
				$s = new survey_result($row["survey_result_id"],$row["survey_id"],$row["survey_result_content"],$row["mark_min"],$row["mark_max"],$row["facebook_survey_result_content"]);
			}
			
			$dbh->commit();
			$dbh = null;
			return $s;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}
	
	function get_survey_result_list()
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$aos = array();
			$stmt = $dbh->query("SELECT * FROM survey_result");
			$rows = $stmt->fetchAll();
			foreach($rows as $row)
			{
				$s = new survey_result($row["survey_result_id"],$row["survey_id"],$row["survey_result_content"],$row["mark_min"],$row["mark_max"],$row["facebook_survey_result_content"]);
				$aos[] = $s;
			}

			$dbh->commit();
			$dbh = null;
			
			return $aos;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function insert_survey_result($s)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("INSERT INTO survey_result (survey_id, survey_result_content, mark_min, mark_max, facebook_survey_result_content)
							VALUE(:survey_id ,:survey_result_content ,:mark_min ,:mark_max ,:facebook_survey_result_content)");
							
							
			$stmt->bindParam(':survey_id', $s->survey_id);
			$stmt->bindParam(':survey_result_content', $s->survey_result_content);
			$stmt->bindParam(':mark_min', $s->mark_min);
			$stmt->bindParam(':mark_max', $s->mark_max);
			$stmt->bindParam(':facebook_survey_result_content', $s->facebook_survey_result_content);
			
			$stmt->execute();
			$id = $dbh->lastInsertId();	
			$dbh->commit();
			$dbh = null;

			return $id;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function update_survey_result($s)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("UPDATE survey_result s 
						SET 
						s.survey_id = :survey_id,
						s.survey_result_content = :survey_result_content,
						s.mark_min = :mark_min,
						s.mark_max = :mark_max,
						s.facebook_survey_result_content = :facebook_survey_result_content
						WHERE 
						s.survey_result_id = :survey_result_id");
			$stmt->bindParam(':survey_id', $s->survey_id);
			$stmt->bindParam(':survey_result_content', $s->survey_result_content);
			$stmt->bindParam(':mark_min', $s->mark_min);
			$stmt->bindParam(':mark_max', $s->mark_max);
			$stmt->bindParam(':facebook_survey_result_content', $s->facebook_survey_result_content);
			$stmt->bindParam(':survey_result_id', $s->survey_result_id);
			
			$stmt->execute();
			
			$dbh->commit();
			$dbh = null;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

}
?>